<?php

/**
 * DeleteAccount form.
 *
 * @package    cpfaizenay
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: DeleteAccountForm.class.php 20147 2009-07-13 11:46:57Z FabianLange $
 */
class DeleteAccountForm extends BaseForm {

    public function configure() {

        $this->setWidgets(array(
          'password'         => new sfWidgetFormInputPassword(array('type' => 'password')),
          'password_again'         => new sfWidgetFormInputPassword(array('type' => 'password'))
        ));

        $this->setValidators(array(
          'password'         => new sfValidatorString(array('max_length' => 128, 'required' => true)),
          'password_again'         => new sfValidatorString(array('max_length' => 128, 'required' => true))
        ));

        $this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_again', array(), array('invalid' => 'Les 2 mots de passe doivent être identiques.')));

        $this->widgetSchema->setFormFormatterName('table');
        $this->widgetSchema->setNameFormat('delete_account[%s]');
    }

    public function deleteConfirm(){
        $user = sfContext::getInstance()->getUser()->getGuardUser();
        $password = $this->getValue('password');

        if($user->checkPassword($password)){

            $user->setIsDelete(true);
            $user->setIsActive(false);
            $user->save();

            return true;
        }else{
            return false;
        }
    }
}
